﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Metriks.Parsiranje;
using System.Windows;

namespace Metriks.NezavisniPutevi
{
    public class Putanja
    {
        public class Link
        {
            public Cvor from { get; set; }
            public Cvor to { get; set; }

            public Link(Cvor from, Cvor to)
            {
                this.from = from;
                this.to = to;
            }

            public bool isSame(Link cmp)
            {
                return (from == cmp.from && to == cmp.to);
            }

            public override string ToString()
            {
                return from.ToString() + "->" + to.ToString();
            }
        }

        private List<Cvor> put;

        public List<Link> edges;

        public Putanja()
        {
            put = new List<Cvor>();
        }

        public Putanja(Putanja postojeća)
        {
            put = new List<Cvor>(postojeća.put);
        }

        public Cvor this[int key]
        {
            get
            {
                return put[key];
            }
            set
            {
                put[key] = value;
            }
        }

        public bool Contains(Cvor p)
        {
            return put.Contains(p);
        }

        public bool Contains(Link l)
        {
            return edges.Count(x => x.isSame(l)) > 0;
        }

        public void Add(Cvor c)
        {
            put.Add(c);
        }

        public override string ToString()
        {
            String rtVar = "";
            foreach (Cvor c in put)
            {
                rtVar += (c.Tip+"->");
            }

            return rtVar.TrimEnd(new char[]{'-','>'});
        }

        public string EdgesToString()
        {
            String debugRtVar = "";
            foreach (Link link in edges){
                debugRtVar += link.ToString() + " ";
            }
            return debugRtVar;
        }

        public void buildLinks()
        {
            edges = new List<Link>();
            foreach (Cvor c in put)
            {
                if (c.Cvorovi.Count>=1 && put.Contains(c.Cvorovi[0]) && edges.Count(x => x.isSame(new Link(c,c.Cvorovi[0])))==0)
                {
                        edges.Add(new Link(c, c.Cvorovi[0]));
                }
                if (c.Cvorovi.Count == 2 && put.Contains(c.Cvorovi[1]) && edges.Count(x => x.isSame(new Link(c,c.Cvorovi[1])))==0)
                {
                    if (c.Tip=="IF" && c.Cvorovi[1].Tip=="ENDIF" && edges.Count(x => x.from==c)>0)
                        continue;
                    else 
                        edges.Add(new Link(c, c.Cvorovi[1]));
                }
            }
        }

        public bool sadrziNajmanjeJedanEdgeKojiNijeUPutanjama(List<Putanja> cmp)
        {
            buildLinks();

            foreach (Putanja tmp in cmp)
            {
                tmp.buildLinks();
            }

            foreach (Link edge in edges)
            {
                bool sadržanEdgeMakarUjednom = false;
                foreach (Putanja putanja in cmp)
                {
                    foreach (Link e2 in putanja.edges)
                    {
                        if (e2.isSame(edge))
                            sadržanEdgeMakarUjednom = true;
                    }
                }
                if (!sadržanEdgeMakarUjednom)
                    return true;
            }

            return false;
        }
    }
}
